package com.echat.messagesender.sender.email.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.echat.messagesender.sender.email.EmailSender;
import com.echat.messagesender.sender.email.bean.EmailConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * accessKey=LTAIKfflaBgs2I9x
 * accessSecret=FVV3YP5gNLjiFBOcScUTvtabNamyxg
 * 如果美西环境使用此工具发送邮件可能需要修改代码(见注释)
 * Created by huangzhilin on 2018/5/9.
 */
public class AliEmailSender extends EmailSender {

    private static final Logger logger = LoggerFactory.getLogger(AliEmailSender.class);

    public static final String serverName = "aliemail";
    private IAcsClient client;
    private EmailConfig config;

    public AliEmailSender(EmailConfig config) {
        this.config = config;
        super.platformAccountId = config.getPlatformAccountId();
        super.platformName = config.getPlatformName();
        buildClient();
    }

    public void buildClient() {
        // 如果是除杭州region外的其它region（如新加坡、澳洲Region），需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"。
        IClientProfile profile = DefaultProfile.getProfile(config.getRegionId(), config.getAccessKey(),  config.getAccessSecret());
        // 如果是除杭州region外的其它region（如新加坡region）， 需要做如下处理
        /*
        try {
        DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm",  "dm.ap-southeast-1.aliyuncs.com");
        } catch (ClientException e) {
        e.printStackTrace();
        }
        */
        this.client = new DefaultAcsClient(profile);
    }

    @Override
    public boolean sendEmail(String[] receives, String subject, String htmlBody, String[] ccReceives, String[] bccReceiver, String replayEmail) {
        for(String receive : receives){
            SingleSendMailRequest request = new SingleSendMailRequest();
            try {
                //request.setVersion("2017-06-22");// 如果是除杭州region外的其它region（如新加坡region）,必须指定为2017-06-22
                request.setAccountName(config.getRainbowredSenderAddress());
                request.setFromAlias(config.getRainbowredSenderNickName());
                request.setAddressType(1);
                request.setTagName(config.getEmailTag());
                request.setReplyToAddress(true);
                request.setToAddress(receive);
                request.setSubject(subject);
                request.setHtmlBody(htmlBody);
                request.setReplyAddress(replayEmail);
                SingleSendMailResponse httpResponse = client.getAcsResponse(request);
                logger.debug("httpResponse.requestId" + httpResponse.getRequestId() + ",httpResponse.envId" + httpResponse.getEnvId());
            } catch (Exception e) {
                logger.error("AliEmailSender.sendEmail.error.", e);
                return false;
            }
        }
        return true;
    }
}
